/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Vistas;
import Modelo.Funciones;
import javax.swing.table.DefaultTableModel;
import Modelo.Usuarios;

public class Usuario extends javax.swing.JInternalFrame {
    
    DefaultTableModel modeloTbUsuarios = new DefaultTableModel();
    public Usuario() {
       initComponents();
      
       this.poblarTbUsuarios();
      
    }
private void poblarTbUsuarios() {
        //lo defino de nuevo para que borre todos los elementos que tiene
        //Sobreescribo el metodo para que no se pueda editar ningun campo
    
    modeloTbUsuarios = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int col) {
                return false;
            }
        };
        //Cabeceras    
        modeloTbUsuarios.addColumn("ID");
        modeloTbUsuarios.addColumn("Usuario");
        modeloTbUsuarios.addColumn("Rol");
        this.desactivar();

        //Body, obtengo los clientes
        Usuarios u = new Usuarios();
        for (Object[] user : u.getUsuarios()) {
           
            
            modeloTbUsuarios.addRow(user);
        }
     
        tbUsuarios.setModel(modeloTbUsuarios);
        //Agrego al modelo para que no se pueda editar
    }

    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jusuario = new javax.swing.JLabel();
        jclave = new javax.swing.JLabel();
        jrol = new javax.swing.JLabel();
        txtusuario = new javax.swing.JTextField();
        cbRol = new javax.swing.JComboBox();
        txtpass = new javax.swing.JPasswordField();
        jPanel2 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnBorrar = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbUsuarios = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setTitle("Usuarios");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Añadir Usuarios", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Tai Le", 1, 14))); // NOI18N

        jusuario.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        jusuario.setText("Usuario:");

        jclave.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        jclave.setText("Clave:");

        jrol.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        jrol.setText("Rol:");

        cbRol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Supervisor", "Ejecutivo", "Administrador" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jclave)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtusuario, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                            .addComponent(txtpass))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jrol)
                        .addGap(18, 18, 18)
                        .addComponent(cbRol, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jusuario)
                        .addGap(57, 57, 57))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jusuario)
                    .addComponent(txtusuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jclave)
                    .addComponent(jrol)
                    .addComponent(cbRol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder()));

        btnAdd.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        btnAdd.setText("Agregar");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnBorrar.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        btnBorrar.setText("Borrar");
        btnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrarActionPerformed(evt);
            }
        });

        btnEdit.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        btnEdit.setText("Editar");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnLimpiar.setFont(new java.awt.Font("Microsoft Tai Le", 1, 12)); // NOI18N
        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnLimpiar)
                .addGap(18, 18, 18)
                .addComponent(btnBorrar)
                .addGap(23, 23, 23)
                .addComponent(btnEdit)
                .addGap(18, 18, 18)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnBorrar)
                    .addComponent(btnEdit)
                    .addComponent(btnLimpiar))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder()));

        tbUsuarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbUsuarios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbUsuariosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbUsuarios);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        if ((txtusuario.getText().trim().length() == 0)||(String.copyValueOf(txtpass.getPassword()).trim().length()==0)) {
            Funciones.mensaje("Debe ingresar todos los datos");
        } else {
        
        Usuarios u = new Usuarios();
        String user= txtusuario.getText();
        String clave = String.copyValueOf(txtpass.getPassword());
        String rol = String.valueOf(cbRol.getSelectedItem());
        // String username, String password, String rol
        u.addUsuario(user, clave, rol);
        this.poblarTbUsuarios();
        }
        
 
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
       if (tbUsuarios.getSelectedRow() > -1) {
        
           if ((txtusuario.getText().trim().length() == 0)||(String.copyValueOf(txtpass.getPassword()).trim().length()==0)) {
            Funciones.mensaje("Debe ingresar todos los datos");
            
            }
           else {
               
            int id = Integer.parseInt(String.valueOf(tbUsuarios.getValueAt(tbUsuarios.getSelectedRow(), 0)));
            Usuarios u = new Usuarios();
            String user = txtusuario.getText();
            String password = String.valueOf(txtpass.getPassword());
            String rol = String.valueOf(cbRol.getSelectedItem());
            u.updateUsuarios(id,user,password,rol);
            
            
           }
            this.poblarTbUsuarios();
            this.vacio();
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrarActionPerformed
        
        
        if ((tbUsuarios.getSelectedRow() > -1)&& (tbUsuarios.getSelectedRow ()!=1)) {
            int id = Integer.parseInt(String.valueOf(tbUsuarios.getValueAt(tbUsuarios.getSelectedRow(), 0)));
            String user = String.valueOf(tbUsuarios.getValueAt(tbUsuarios.getSelectedRow(), 1));
            Usuarios u = new Usuarios();
 
            if (u.existeUsername(user)) {
                u.deleteUsuario(id);
                this.poblarTbUsuarios();
 
            } else {
                Funciones.mensaje("El usuario no existe");
                this.poblarTbUsuarios();
            }
        }
    }//GEN-LAST:event_btnBorrarActionPerformed

    private void tbUsuariosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbUsuariosMouseClicked
        this.activar();
        if (tbUsuarios.getSelectedRow() > -1) {
            String rol = String.valueOf(tbUsuarios.getValueAt(tbUsuarios.getSelectedRow(), 2));
            txtusuario.setText(String.valueOf(tbUsuarios.getValueAt(tbUsuarios.getSelectedRow(), 1)));
            cbRol.setSelectedItem(Character.toString(Character.toUpperCase(rol.charAt(0)))+rol.substring(1));
        }
           
    }//GEN-LAST:event_tbUsuariosMouseClicked

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        this.vacio();
        
        
    }//GEN-LAST:event_btnLimpiarActionPerformed
    private void desactivar (){
   
    btnBorrar.setEnabled(false);
    
    btnEdit.setEnabled(false);
    }
    private void activar (){
    
    btnBorrar.setEnabled(true);
    
    btnEdit.setEnabled(true);
    }
    private void vacio (){
    txtusuario.setText("");
    txtpass.setText("");
        this.poblarTbUsuarios();
    }
    
       
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnBorrar;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cbRol;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jclave;
    private javax.swing.JLabel jrol;
    private javax.swing.JLabel jusuario;
    private javax.swing.JTable tbUsuarios;
    private javax.swing.JPasswordField txtpass;
    private javax.swing.JTextField txtusuario;
    // End of variables declaration//GEN-END:variables
}
